package com.zztl.infiniteBand.ui.activity

import android.app.Activity
import android.content.Intent
import android.text.Editable
import android.text.InputType
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.widget.TextView
import butterknife.OnClick
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.base.BaseMvpActivity
import com.zztl.infiniteBand.net.requestBean.GetVerifyRequest
import com.zztl.infiniteBand.net.requestBean.RegisterRequest
import com.zztl.infiniteBand.presenter.RegisterPresenter
import com.zztl.infiniteBand.storage.BillPreference
import com.zztl.infiniteBand.storage.Constans
import com.zztl.infiniteBand.storage.PreferenceKeys
import com.zztl.infiniteBand.ui.view.CommonItemView
import com.zztl.infiniteBand.ui.view.CommonItemView2
import com.zztl.infiniteBand.ui.view.mvpview.RegisterView
import com.zztl.infiniteBand.util.ToastUtils
import com.zztl.infiniteBand.util.toast
import kotlinx.android.synthetic.main.activity_register.*
import kotlinx.android.synthetic.main.activity_verify_phone.*

class RegisterActivity : BaseMvpActivity<RegisterView, RegisterPresenter>(), RegisterView, CommonItemView.OnItemRightClickListener, TextWatcher {


/*    @BindView(R.id.item_phone)
    lateinit var item_phone: CommonItemView
    @BindView(R.id.item_pwd)
    lateinit var item_pwd: CommonItemView
    @BindView(R.id.item_code)
    lateinit var item_code: CommonItemView*/


    private lateinit var mRegisterPresenter: RegisterPresenter
    private lateinit var mRightTv: TextView

    override fun setLayoutId(): Int = R.layout.activity_register

    var isLook: Boolean = false

    override fun initBase() {
        item_code.setItemRightClickListener(this)
        mRightTv = item_code.rightTv
        item_phone.middleEdit.addTextChangedListener(this)
        item_pwd.middleEdit.addTextChangedListener(this)
        item_code.middleEdit.addTextChangedListener(this)

        item_pwd.setRightImg(R.mipmap.biyanjing)

        item_pwd.setItemRightClickListener(object : CommonItemView2.OnItemRightClickListener {
            override fun onClickItemRight(viewID: Int) {
                isLook = !isLook.apply {
                    if (this) {
                        item_pwd.middleEdit.inputType = InputType.TYPE_CLASS_TEXT
                        item_pwd.setRightImg(R.mipmap.zhengyanjing)
                    } else {
                        item_pwd.middleEdit.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
                        item_pwd.setRightImg(R.mipmap.biyanjing)
                    }
                }
            }
        })
        item_phone.leftImgTv.apply {
            text = "+${BillPreference.getApp(PreferenceKeys.COUNTYRCODE.name, "86") as String}"
            setOnClickListener(object : View.OnClickListener {
                override fun onClick(v: View?) {
                    val intent = Intent(this@RegisterActivity, CountryCodeActivity::class.java)
                    showActivityForResult(intent, Constans.COUNTRY_CODE)
                }

            })
        }

        //设置EditText的显示方式为多行文本输入
        item_code.middleEdit.run {
            inputType = InputType.TYPE_TEXT_FLAG_MULTI_LINE
            setSingleLine(false)
            setHorizontallyScrolling(false)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        data?.let {
            if (Constans.COUNTRY_CODE == requestCode && resultCode == Activity.RESULT_OK) {
                item_phone.leftImgTv.text = "+${data.getStringExtra(Constans.COUNTRY_CODE_STR)}"
            }
        }
    }


    override fun createPresenter(): RegisterPresenter {
        mRegisterPresenter = RegisterPresenter()
        return mRegisterPresenter
    }


    override fun afterTextChanged(s: Editable?) {
        s?.let {
            btn_register.isEnabled = item_phone.middleEditStr.isNotEmpty() && item_pwd.middleEditStr.length >= 6 && item_code.middleEditStr.length == 4
        }
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
    }


    @OnClick(R.id.btn_register, R.id.iv_return,R.id.btn_login)
    fun click(view: View) {
        when (view.id) {
            R.id.btn_register -> if (mRegisterPresenter.check(item_phone.middleEditStr, item_pwd.middleEditStr, item_code.middleEditStr,item_name.middleEditStr)) {
                mRegisterPresenter.register(RegisterRequest(item_phone.middleEditStr, item_pwd.middleEditStr, item_code.middleEditStr, item_name.middleEditStr), this)
            }
            R.id.iv_return -> finish()
            R.id.btn_login->finish()
        }
    }

    override fun registerSuccess() {
        BillPreference.putApp(PreferenceKeys.DEFAULT_PHONE.name, item_phone.middleEditStr)
        toast(getString(R.string.注册成功))
        finish()
    }

    override fun registerFail() {

    }

    override fun getCodeFail() {
        mRegisterPresenter.resetSmsView()
    }

    override fun setDisableView(count: Int) {
        mRightTv.isClickable = false
        mRightTv.text = "(" + count + "S)" + getString(R.string.重新发送)
        mRightTv.setTextColor(resources.getColor(R.color.color_ACA593))
    }

    override fun setUseableView() {
        mRightTv.isClickable = true
        mRightTv.text = getString(R.string.发送验证码)
        mRightTv.setTextColor(resources.getColor(R.color.color_007aff))
    }

    override fun getCodeSuccess() {

    }


    override fun onClickItemRight(viewID: Int) {
        if (TextUtils.isEmpty(item_phone.middleEditStr)) {
//            ToastUtils.show(this, getString(R.string.请输入手机号))
           toast(getString(R.string.请输入手机号))
        } else {
            mRegisterPresenter.startCountDown(this)
            mRegisterPresenter.getCode(GetVerifyRequest(item_phone.middleEditStr, "0", item_phone.leftImgTv.text.toString()), this)
        }
    }


    override fun onDestroy() {
        mRegisterPresenter.destoryTimer()
        super.onDestroy()
    }
}
